
package com.coolgor.coldot.service.wechat.gzh.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.wechat.gzh.GZHUser;
import com.coolgor.coldot.dao.wechat.gzh.GZHUserDao;
import com.coolgor.coldot.dto.wechat.gzh.GZHUserExecution;
import com.coolgor.coldot.enums.wechat.gzh.GZHUserOperationStateEnum;
import com.coolgor.coldot.exceptions.wechat.gzh.GZHUserOperationException;
import com.coolgor.coldot.service.wechat.gzh.GZHUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class GZHUserServiceImpl implements GZHUserService {

	private static Logger log = LoggerFactory.getLogger(GZHUserServiceImpl.class);

	@Autowired
	GZHUserDao gzhUserDao;

	@Override
	public GZHUserExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL_ID);
		}
		GZHUser gZHUser = gzhUserDao.queryByID(recordID);
		if (gZHUser != null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS, gZHUser);
		} else {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		}
	}

	@Override
	public GZHUserExecution getList(GZHUser gzhUserCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<GZHUser> list = gzhUserDao.queryList(gzhUserCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = gzhUserDao.queryCount(gzhUserCondition);
		GZHUserExecution result = new GZHUserExecution();
		if (list != null) {
			result.setState(GZHUserOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(GZHUserOperationStateEnum.SUCCESS.getStateInfo());
			result.setGZHUserList(list);
			result.setCount(count);
		} else {
			result.setState(GZHUserOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public GZHUserExecution add(GZHUser gzhUser) throws GZHUserOperationException {
		// 空值判断
		if (gzhUser == null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			gzhUser.setCreateTime(new Date());
			gzhUser.setLastEditTime(new Date());
			// 执行插入数据操作 
			effectedNum = gzhUserDao.insert(gzhUser);
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser信息插入失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Add GZHUser error:" + e.getMessage());
		}
		return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS, gzhUser);
	}

	@Override
	@Transactional
	public GZHUserExecution modify(GZHUser gzhUser) throws GZHUserOperationException {
		// 空值判断
		if (gzhUser == null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 把记录的最后更新时间设置为当前时间 
			gzhUser.setLastEditTime(new Date());
			// 执行修改更新操作 
			effectedNum = gzhUserDao.update(gzhUser);
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser信息更新失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Update gzhUser error:" + e.getMessage());
		}
		return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS, gzhUser);
	}

	@Override
	@Transactional
	public GZHUserExecution uptodate(GZHUser gzhUser) throws GZHUserOperationException {
		// 空值判断
		if ( null == gzhUser
				|| null == gzhUser.getAppID()
				|| "".equals(gzhUser.getAppID())
				|| null == gzhUser.getOpenid()
				|| "".equals(gzhUser.getOpenid())
		) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		}
		int effectedNum = -1;
		try {
			GZHUser condition = new GZHUser();
			condition.setAppID(gzhUser.getAppID());
			condition.setOpenid(gzhUser.getOpenid());
			List<GZHUser> list = gzhUserDao.queryList(condition, 0, 1);
			if (null == list || list.size()==0 ) {
				// 执行插入操作
				gzhUser.setStatus(0); // 0禁止，1允许，设置用户第一次登陆时状态为禁止登陆
				gzhUser.setCreateTime(new Date());
				gzhUser.setLastEditTime(new Date());
				effectedNum = gzhUserDao.insert(gzhUser);
			} else {
				// 执行修改更新操作
				gzhUser.setRecordID(list.get(0).getRecordID());
				gzhUser.setLastEditTime(new Date());
				effectedNum = gzhUserDao.update(gzhUser);
			}
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("OAuth2AccessToken up to date error!");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Up to date oAuth2AccessToken error:" + e.getMessage());
		}
		return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS, gzhUser);
	}

	@Override
	@Transactional
	public GZHUserExecution remove(Long recordID) throws GZHUserOperationException{
		// 空值判断
		if (recordID == null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gzhUserDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser单条信息删除失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Delete gZHUser error:" + e.getMessage());
		}
		return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public GZHUserExecution remove(GZHUser gzhUser) throws GZHUserOperationException{
		// 空值判断
		if (gzhUser == null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gzhUserDao.delete(gzhUser.getRecordID());
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser单条信息删除失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Delete gzhUser error:" + e.getMessage());
		}
		return new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS, gzhUser);
	}

	@Override
	@Transactional
	public GZHUserExecution removeBatchByID(List<Long> recordIDList) throws GZHUserOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gzhUserDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Batch Delete gZHUser error:" + e.getMessage());
		}
		GZHUserExecution exe = new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public GZHUserExecution removeBatch(List<GZHUser> gZHUserList) throws GZHUserOperationException{
		// 空值判断
		if (gZHUserList == null) {
			return new GZHUserExecution(GZHUserOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<gZHUserList.size(); i++) {
			recordIDList.add(gZHUserList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = gzhUserDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new GZHUserOperationException("GZHUser多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new GZHUserOperationException("Batch Delete gZHUser error:" + e.getMessage());
		}
		GZHUserExecution exe = new GZHUserExecution(GZHUserOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
